package cn.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.bean.Film_info;
import cn.bean.Photo;
import cn.dao.PhotoDao;
import cn.dbc.BaseDao;

public class PhotoDaoImpl implements PhotoDao {

	BaseDao bs = new BaseDao();
	Film_infoDaoImpl fi = new Film_infoDaoImpl();
	@Override
	public Photo findPhotoById(int id) {
		Photo p = new Photo();
		Connection conn = bs.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		if(conn!=null){
			String sql = "select * from photo where fk_info_id = "+id;
			try {
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				if(rs.next()){
					
					p.setId(rs.getInt(1));
					p.setName(rs.getString(2));
					p.setInfo_id(fi.findFilmById(rs.getInt(3)));
					
					
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			finally{
				bs.closeConnection(conn, pstmt, rs);
			}
		}
		
		
		return p;
	}

	@Override
	public List<Photo> findAllPhoto() {
		List<Photo> list = new ArrayList<Photo>();
		Connection conn = bs.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		if(conn!=null){
			String sql = "select * from photo";
			try {
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				while(rs.next()){
					Photo p = new Photo();
					p.setName(rs.getString(1));
					p.setInfo_id(fi.findFilmById(rs.getInt(2)));
					list.add(p);
					
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			finally{
				bs.closeConnection(conn, pstmt, rs);
			}
		}
		
		
		return list;
	}

	@Override
	public boolean doPhoto(String name,Film_info fi) {
		boolean isFlag=false;
		String sql="insert into photo(name,fk_info_id) values(?,?)";
		List<Object> lo=new ArrayList<Object>();
		lo.add(name);
		lo.add(fi.getId());
		isFlag=bs.update(sql, lo);
		return isFlag;
	}

	@Override
	public boolean delPhoto(int id) {
		boolean isFlag=false;
		String sql="delete from photo where fk_info_id=?";
		List<Object> lo=new ArrayList<Object>();
		lo.add(id);
		isFlag=bs.update(sql, lo);
		return isFlag;
	}

	@Override
	public boolean upPhoto(String name,int id) {
		boolean isFlag=false;
		String sql="update photo set name =? where fk_info_id=?";
		List<Object> lo=new ArrayList<Object>();
		lo.add(name);
		lo.add(id);
		isFlag=bs.update(sql, lo);
		return isFlag;
	}

	@Override
	public Photo findPhotoByI(int id) {
		Photo p = new Photo();
		Connection conn = bs.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		if(conn!=null){
			String sql = "select * from photo where id ="+id;
			try {
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				if(rs.next()){
					p.setId(rs.getInt(1));
					p.setName(rs.getString(2));
					p.setInfo_id(fi.findFilmById(rs.getInt(3)));
					
					
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			finally{
				bs.closeConnection(conn, pstmt, rs);
			}
		}
		return p;
	}

}
